/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.delivery.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.OfflineHandleEventType;
import com.yami.shop.bean.model.OfflineHandleEvent;
import com.yami.shop.bean.param.OfflineHandleEventAuditParam;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.bean.model.Station;
import com.yami.shop.service.StationService;
import com.yami.shop.security.platform.util.SecurityUtils;
import com.yami.shop.service.OfflineHandleEventService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 自提点
 *
 * @author YXF
 * @date 2020-04-23 15:18:29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/platform/station" )
public class StationController {

    private final StationService stationService;

    private final OfflineHandleEventService offlineHandleEventService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param station 自提点
     * @return 分页数据
     */
    @GetMapping("/page" )
    public ResponseEntity<IPage<Station>> getStationPage(PageParam<Station> page, Station station) {
        return ResponseEntity.ok(stationService.getPage(page, station));
    }


    /**
     * 通过id查询自提点
     * @param stationId id
     * @return 单个数据
     */
    @GetMapping("/info/{stationId}" )
    public ResponseEntity<Station> getById(@PathVariable("stationId") Long stationId) {
        return ResponseEntity.ok(stationService.getStationInfoById(stationId));
    }


    /**
     * 下线活动
     */
    @PostMapping("/offline")
    @PreAuthorize("@pms.hasPermission('platform:station:update')" )
    public ResponseEntity<Void> offline(@RequestBody OfflineHandleEvent offlineHandleEvent) {
        Station station = stationService.getById(offlineHandleEvent.getHandleId());

        if (station == null) {
            // 未找到该自提点信息
            throw new YamiShopBindException("yami.station.no.find");
        }
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        stationService.offline(station, offlineHandleEvent.getOfflineReason(), sysUserId);
        // 移除缓存
        stationService.removeCacheByStationId(station.getStationId());
        return ResponseEntity.ok().build();
    }

    /**
     * 通过活动id获取下线信息
     */
    @GetMapping("/getOfflineHandleEventByStationId/{stationId}")
    public ResponseEntity<OfflineHandleEvent> getOfflineHandleEventByDiscountId(@PathVariable("stationId") Long stationId) {
        OfflineHandleEvent offlineHandleEvent = offlineHandleEventService.getProcessingEventByHandleTypeAndHandleId(OfflineHandleEventType.STATION.getValue(), stationId);
        return ResponseEntity.ok(offlineHandleEvent);
    }


    /**
     * 审核活动
     */
    @PostMapping("/auditStation")
    @PreAuthorize("@pms.hasPermission('platform:station:update')")
    public ResponseEntity<Void> auditSeckill(@RequestBody OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        Station station = stationService.getById(offlineHandleEventAuditParam.getHandleId());
        if (station == null) {
            // 未找到该自提点信息
            throw new YamiShopBindException("yami.station.no.find");
        }
        stationService.auditSeckill(offlineHandleEventAuditParam, sysUserId);

        // 移除缓存
        stationService.removeCacheByStationId(station.getStationId());
        return ResponseEntity.ok().build();
    }

}
